package com.gtt.jr.action.admin;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import com.gtt.jr.action.base.BaseModelAction;
import com.gtt.jr.action.model.ContainerAddModel;
import com.gtt.jr.app.ContainerRepository;
import com.gtt.jr.app.domain.ContainerVO;
import com.gtt.jr.util.ImageUtils;

/**
 * 集装箱添加
 * 
 * @author 高甜甜(gao12581@sina.com)
 * @date 2010-9-11
 * 
 */
public class ContainerAddExecAction extends BaseModelAction<ContainerAddModel> {

	private static final long serialVersionUID = -422882414901590261L;

	@Resource
	private ContainerRepository containerRepository;

	@Override
	public String execute() throws Exception {
		containerRepository.add(convert(getModel()));
		return JSON;
	}

	private ContainerVO convert(ContainerAddModel model) throws IOException {
		ContainerVO vo = new ContainerVO();
		vo.setType(model.getType());
		vo.setName(model.getName());
		vo.setDescription(model.getDescription());
		Map<String, String> picture = ImageUtils.savePicture(model.getUpload(),
				model.getSuffix(), getRealPath());
		vo.setUrl(picture.get("url"));
		vo.setSurl(picture.get("surl"));
		vo.setTurl(picture.get("turl"));
		return vo;
	}

}
